package com.haochang.chunkvod.view.vod.dialog;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseGridView;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.tools.vod.other.GridViewUtil;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * 添加现场排号的dialog
 *
 * @author Yangjie
 *         className ChooseRoomDialog
 *         created at  2016/11/2  17:43
 */

public class AddReserveRoomDialog extends Dialog {
    public AddReserveRoomDialog(Context context) {
        super(context);
    }

    public AddReserveRoomDialog(Context context, int theme) {
        super(context, theme);
    }

    protected AddReserveRoomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private CharSequence title; //对话框标题
        private List<HostessInfo> dataList;
        private OnResultListener onResultListener; //对话框回调事件
        private QuickAdapter quickAdapter = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(int title) {
            this.title = context.getText(title);
            return this;
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public Builder setDataList(List<HostessInfo> dataList) {
            this.dataList = dataList;
            for (HostessInfo info : dataList) {
                info.setSelected(false);
            }
            return this;
        }

        public List<HostessInfo> getDataList() {
            return dataList;
        }

        public Builder setOnResultListener(OnResultListener onResultListener) {
            this.onResultListener = onResultListener;
            return this;
        }

        private LinearLayout layout;//inflate的view
        private HostessInfo itemTempType = null;
        private HostessInfo itemTempRoom = null;

        /**
         * 根据规定的最大条数显示列表
         *
         * @param rows 最大显示条数
         * @return ChooseActivityDialog
         */
        public AddReserveRoomDialog createDialog(int rows) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final AddReserveRoomDialog dialog = new AddReserveRoomDialog(context, R.style.Alert_Dialog);
            final ViewGroup viewGroup = null;
            final BaseGridView gridView = (BaseGridView) layout.findViewById(R.id.grid_view);
            final BaseTextView tv_counts_of_member = (BaseTextView) layout.findViewById(R.id.tv_counts_of_member);
            layout = (LinearLayout) inflater.inflate(R.layout.inflate_add_reserve_room_dialog, viewGroup, false);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            if (rows == 0) {
                gridView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, DipPxConversion.dipToPx(context, 40 * dataList.size())));
            } else {
                //当数据条数小于设置最大显示条数时，高度设置为实际高度，当大于设置条数时，显示设置条数的高度
                gridView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, DipPxConversion.dipToPx(context, 41 * rows)));
            }
            tv_counts_of_member.setOnClickListener(new OnBaseClickListener() {
                @Override
                public void onBaseClick(View v) {
                    KeyBoardDialog keyBoardDialog = new KeyBoardDialog(context);
                    keyBoardDialog.setKeyBoardOnSureListener(new KeyBoardDialog.KeyBoardOnSureListener() {
                        @Override
                        public void setValue(String value) {
                            tv_counts_of_member.setText(value);
                        }
                    });
                    keyBoardDialog.show();
                }
            });
            quickAdapter = new QuickAdapter<HostessInfo>(context, R.layout.item_part_modify_grid_layout, dataList) {
                @Override
                protected void convert(BaseAdapterHelper helper, final HostessInfo item) {
                    helper.getView(R.id.img_del).setVisibility(View.GONE);
                    helper.setText(R.id.tv_partName, item.getRoomNum());
                    if (item.isSelected())//这个用于显示是否选中
                    {
                        helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn_red);
                        ((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn1);
                        ((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(context.getResources().getColor(R.color.color_282828));
                    }

                    helper.getView(R.id.tv_partName).setOnClickListener(new OnBaseClickListener() {
                        @Override
                        public void onBaseClick(View v) {
                            if (null != itemTempRoom)
                                itemTempRoom.setSelected(false);
                            itemTempRoom = item;
                            item.setSelected(true);
                            quickAdapter.notifyDataSetChanged();
                        }
                    });
                }
            };
            gridView.setAdapter(quickAdapter);
            GridViewUtil.setListViewHeightBasedOnChildren(context, gridView);
            quickAdapter.notifyDataSetChanged();

            BaseTextView tv_title = (BaseTextView) layout.findViewById(R.id.tv_title);
            tv_title.setText(title);
            layout.findViewById(R.id.tv_ensure).setOnClickListener(new OnBaseClickListener() {
                @Override
                public void onBaseClick(View v) {
                    if (TextUtils.isEmpty(tv_counts_of_member.getText().toString())) {
                        ToastUtils.showText(context, "请先输入人数!");
                        return;
                    }else if(itemTempType==null){
                        ToastUtils.showText(context, "请先选择房型!");
                        return;
                    }

                    if (onResultListener != null) {
                        if (itemTempRoom != null) {
                            onResultListener.onResult(itemTempRoom, Integer.parseInt(tv_counts_of_member.getText().toString()));
                        }
                        dialog.dismiss();
                    }
                }
            });
            layout.findViewById(R.id.tv_cancel).setOnClickListener(new OnBaseClickListener() {
                @Override
                public void onBaseClick(View v) {
                    dialog.dismiss();
                }
            });
            return dialog;
        }

        public void showDialog(int rows) {
            AddReserveRoomDialog dialog = createDialog(rows);
            dialog.show();
            Window window = dialog.getWindow();
            if (window != null) {
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.width = ScreenUtils.getScreenWidth(context) - 2 * DipPxConversion.dip2px(context, 20);
                window.setAttributes(layoutParams);
                window.setGravity(Gravity.CENTER);
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setContentView(layout);
                dialog.setCancelable(true);
                dialog.setCanceledOnTouchOutside(false);
            }
        }
    }

    public interface OnResultListener {
        void onResult(HostessInfo info, int number);
    }
}
